package imagemanip.png;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class PNG {
    public static Color[][] importImage(String f){
        BufferedImage img = null;
	try {
            img = ImageIO.read(new File(f));
        } catch (IOException e) {
	    return null;
        }

	Color[][] mat = new Color[img.getWidth(null)][img.getHeight(null)];
        for (int x=0; x < img.getWidth(null); x++)
            for (int y=0; y < img.getHeight(null); y++)
		mat[x][y] = new Color(img.getRGB(x,y));

	return mat;
    }

    public static void exportImage(String f, Color [][] mat){
	BufferedImage bi = new BufferedImage(mat.length, mat[0].length,
					     BufferedImage.TYPE_INT_RGB );

        for (int x=0; x < bi.getWidth(null); x++)
            for (int y=0; y < bi.getHeight(null); y++)
		bi.setRGB(x, y, mat[x][y].getRGB());

	try {
            File outputfile = new File(f);
            ImageIO.write(bi, "png", outputfile);
        } catch (IOException e) {
        }
    }
}